package org.oslims.client.uicomponents.editbox;

import java.util.HashMap;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.SelectManyBox;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Analysis;
import org.oslims.domain.SampleType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditSampleTypeBox {

	final DialogBox editSampleTypeBox = new DialogBox(false, true);
	final VerticalPanel editSampleTypeBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField nameField = new TextInputField("name", "Name: ", "80px", "200px");
	final SelectManyBox<Analysis> analysisSelectionField = new SelectManyBox<Analysis>("Analysis: ");

	private SampleType sampleType;

	String editedType;

	public EditSampleTypeBox() {

		editSampleTypeBoxPanel.add(nameField);
		inputFields.put(nameField.getFieldName(), nameField);

		editSampleTypeBoxPanel.add(analysisSelectionField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editSampleTypeBox.add(editSampleTypeBoxPanel);

		// Add the save and cancel buttons
		editSampleTypeBoxPanel.add(buttonLine);
	}

	public void hide() {
		editSampleTypeBox.hide();
	}

	public void clear() {
		sampleType = null;
		nameField.getInputField().setValue("");
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();

		editSampleTypeBox.setText("Create new SampleType");
		editSampleTypeBox.center();
	}

	public void showEdit(SampleType st) {
		this.clear();
		this.clearAllErrors();

		sampleType = st;
		nameField.getInputField().setValue(st.getName());
		editSampleTypeBox.setText("Edit SampleType");
		editSampleTypeBox.center();
	}

	public void showError(String fieldName, String message) {
		GWT.log(fieldName);
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getName() {
		return nameField.getInputField();
	}

	public SelectManyBox<Analysis> getAnalysisSelection() {
		return analysisSelectionField;
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public void setSampleType(SampleType sampleType) {
		this.sampleType = sampleType;
	}

	public SampleType getSampleType() {
		return sampleType;
	}
}
